<script setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import api from '@/api'
import { Message } from '@arco-design/web-vue'

const $route = useRoute()
const $router = useRouter()

const order_no = ref('')
const order_status = ref('') // 订单状态 1:等待发起2:待支付3:已支付
const pageVal = ref(1)
const pageSize = ref(10)
const listTotal = ref(0)
const listData = ref()
const formVisible1 = ref(false)
const formVisible2 = ref(false)
const formData1 = ref({
  id: null,
  order_no: '',
  user_id: '',
  order_type: '',
  order_status: '',
  order_price: '',
  created_at: '',
  custom_name: '',
  company_name: '',
  order_user: '',
})
const formData2 = ref({
  id: null,
  price: '',
})

const getListData = async (page) => {
  try {
    const res = await api.get('system/order/list', {
      params: {
        order_no: order_no.value,
        order_status: order_status.value,
        page,
        pageSize: pageSize.value,
      },
    })
    listData.value = res.data.items
    listTotal.value = res.data.pageInfo.total
    pageVal.value = page
  } catch (error) {
    console.error(error)
  }
}

const showFormData1 = async (record) => {
  if (record) {
    formData1.value.id = record.id
    switch (record.order_type * 1) {
      case 1:
        formData1.value.order_type_text = '课程定制'
        break
      case 2:
        formData1.value.order_type_text = '课程购买'
        break
    }
    formData1.value.order_file = record.order_file
    formData1.value.order_content = record.order_content
    formData1.value.contacts = record.contacts
  }
  formVisible1.value = true
}

const showFormData2 = async (record) => {
  if (record) {
    formData2.value.id = record.id
  }
  formVisible2.value = true
}
const formSubmit2 = async () => {
  var obj = { ...formData2.value }
  if (!obj.price) {
    Message.error('价格不可为空')
    return false
  }
  try {
    const res = await api.post('system/order/startPay/' + obj.id, obj)
    Message.success(res.message)
    formVisible2.value = false
    getListData(1)
    resetFormData2()
  } catch (error) {
    console.error(error)
  }
}

const resetFormData2 = async () => {
  formData2.value = {
    id: null,
    price: '',
  }
}

getListData(1)
</script>

<template>
  <div class="page">
    <div class="header">
      <a-space>
        <a-select v-model="order_status"
                  :style="{width:'240px'}"
                  placeholder="请选择订单状态">
          <a-option value="">全部订单</a-option>
          <a-option value="0">等待发起</a-option>
          <a-option value="1">待支付</a-option>
          <a-option value="2">已支付</a-option>
        </a-select>

        <a-input :style="{ width: '240px' }"
                 v-model="order_no"
                 placeholder="按订单编号查询"
                 allow-clear />

        <a-button @click="getListData(1)"
                  type="primary">查询</a-button>
      </a-space>
    </div>

    <a-table :data="listData"
             @page-change="getListData"
             :pagination="{
              showTotal: true,
              size: 'small',
              total: listTotal,
              pageSize: pageSize,
              current: pageVal,
            }">
      <template #columns>
        <a-table-column title="序号"
                        :width="60">
          <template #cell="{ rowIndex }"> {{ rowIndex + 1 }} </template>
        </a-table-column>
        <a-table-column title="订单编号"
                        data-index="order_no"></a-table-column>
        <a-table-column title="公司名称"
                        data-index="company_name"></a-table-column>
        <a-table-column title="订单类型">
          <template #cell="{ record }">
            <div v-if="record.order_type == 1">课程定制</div>
            <div v-if="record.order_type == 2">课程购买</div>
          </template>
        </a-table-column>
        <a-table-column title="订单时间"
                        data-index="created_at"></a-table-column>
        <a-table-column title="订单金额"
                        data-index="order_price"></a-table-column>
        <a-table-column title="订单状态">
          <template #cell="{ record }">
            <div v-if="record.order_status == 0">等待发起</div>
            <div v-if="record.order_status == 1">待支付</div>
            <div v-if="record.order_status == 2">已支付</div>
          </template>
        </a-table-column>
        <a-table-column title="操作"
                        align="right"
                        :width="300">
          <template #cell="{ record }">
            <a-space>
              <a-button v-if="record.order_status == 0"
                        @click="showFormData2(record)"
                        type="outline"
                        size="mini">发起支付</a-button>
              <a-button @click="showFormData1(record)"
                        type="primary"
                        size="mini">查看详情</a-button>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <a-modal title="订单详情"
             width="600px"
             :footer="false"
             v-model:visible="formVisible1">
      <a-form>
        <a-form-item label="订单类型">
          <!-- <a-select v-model="formData.order_type" :style="{width:'400px'}" placeholder="请选择订单类型">
            <a-option :value="1">课程定制</a-option>
            <a-option :value="2">课程购买</a-option>
          </a-select> -->
          <a-input readonly
                   v-model="formData1.order_type_text"
                   :style="{ width: '400px' }"
                   placeholder="" />
        </a-form-item>
        <a-form-item label="上传文件">
          <a-link :href="formData1.order_file"
                  icon>文件地址</a-link>
        </a-form-item>
        <a-form-item label="订单需求">
          <a-textarea readonly
                      v-model="formData1.order_content"
                      :style="{ width: '400px' }"
                      placeholder="请输入订单需求" />
        </a-form-item>
        <a-form-item label="联系人">
          <div class="flex fd_c">
            <div v-for="(item, index) in formData1.contacts"
                 :key="index"
                 class="mb10 flex at_c">
              <a-input-group class="mr10">
                <a-input readonly
                         v-model="item.name"
                         :style="{ width: '200px' }"
                         placeholder="请输入联系人姓名" />
                <a-input readonly
                         v-model="item.mobile"
                         :style="{ width: '200px' }"
                         placeholder="请输入联系人电话" />
              </a-input-group>
            </div>
          </div>
        </a-form-item>
      </a-form>
    </a-modal>

    <a-modal title="发起支付"
             width="600px"
             :on-before-ok="formSubmit2"
             v-model:visible="formVisible2">
      <a-form>
        <a-form-item label="价格">
          <a-input-number v-model="formData2.price"
                          :style="{width:'420px'}"
                          placeholder="请输入价格"
                          :min="0" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<style scoped>
.page {
  height: 100%;
}

.header {
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>